package cn.yjr.vector;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.yjr.vector.tools.Constants;
import cn.yjr.vector.tools.MD5;
import cn.yjr.vector.tools.SharedPreferencesHelper;
import cn.yjr.vector.tools.YJRApplication;
import cn.yjr.vector.tools.YJRHttpClient;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;

public class CareerInfoActivity extends SherlockActivity implements OnClickListener{

	LayoutInflater inflater;
	ActionBar actionBar;
	View title_bar;
	TextView forget_pwd_tv;
	Context context;
	String response;
	JSONObject jsonInfo;
	JSONObject userData = null, initData = null;
	String phone, pwd;
	String hospital_id;
	String title_type;
	String pc_path,qc_path,wc_path;
	static final int LOGIN_MSG = 0;
	RelativeLayout zgzs_rl, zyzs_rl, gzz_rl, tdjs_rl, zytc_rl, xw_rl, yx_rl, hospital_rl, department_rl, position_rl;
	SharedPreferencesHelper spHelper;
	TextView hospital_tv,department_tv, title_tv,applycheck;
	String hospital, department, title;
	ImageView zgzs_imv,zyzs_imv,gzz_imv;
	String category;
	static final int DEGREE = 1;
	static final int CATEGORY = 2;
	static final int HOSPITAL = 3;
	static final int DEPARTMENT = 4;
	String status;
	DisplayImageOptions options;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	ImageSize minImageSize = new ImageSize(40, 40);
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_career_info);
		inflater = this.getLayoutInflater();
		actionBar = getSupportActionBar();
		context = this;
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.me_jobinfo)
		.showImageForEmptyUri(R.drawable.me_appointment)
		.showImageOnFail(R.drawable.head)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();
		this.spHelper = new SharedPreferencesHelper(this.context);
		Intent intent = this.getIntent();
		initData = YJRApplication.initData;
		try {
			userData = new JSONObject(intent.getStringExtra("userData"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		try {
			initData();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		initTitleBar();
		
		initComponent();
		
		
		
		
	
	}
	
	/**
	 * @throws JSONException 
	 * 
	 */
	private void initData() throws JSONException {
		hospital = null;
		department = null;
		title = null;
		category = userData.getString("category");
		pc_path = userData.getString("pcPath");
		qc_path = userData.getString("qcPath");
		wc_path = userData.getString("wcPath");
		status = userData.getString("status");
		JSONObject hospital_json = userData.getJSONObject("hospital");
		hospital = hospital_json.getString("text");
		department = userData.getJSONObject("section").getString("text");
		title_type = userData.getString("title");
		title = initData.getJSONObject("codings").getJSONObject("doctorCategory").getJSONObject(category).getJSONObject("title").getString(title_type);
		
		
		
	}

	private void initTitleBar(){
		ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.WRAP_CONTENT,Gravity.CENTER);
		title_bar = inflater.inflate(R.layout.career_info_action_bar, null);
		
		actionBar.setCustomView(title_bar,lp);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);
	}
	private void initComponent(){
		zgzs_rl = (RelativeLayout) findViewById(R.id.zgzs);
		zyzs_rl = (RelativeLayout) findViewById(R.id.zyzs);
		gzz_rl = (RelativeLayout) findViewById(R.id.gzz);
		zgzs_rl.setOnClickListener(this);
		zyzs_rl.setOnClickListener(this);
		gzz_rl.setOnClickListener(this);
		
		
		
		tdjs_rl  =(RelativeLayout) findViewById(R.id.tdjs_rl);
		zytc_rl  =(RelativeLayout) findViewById(R.id.zytc_rl);
		xw_rl  =(RelativeLayout) findViewById(R.id.xw_rl);
		yx_rl  =(RelativeLayout) findViewById(R.id.yx_rl);
		tdjs_rl.setOnClickListener(this);
		zytc_rl.setOnClickListener(this);
		xw_rl.setOnClickListener(this);
		yx_rl.setOnClickListener(this);
		
		hospital_rl  =(RelativeLayout) findViewById(R.id.hospital_rl);
		department_rl  =(RelativeLayout) findViewById(R.id.department_rl);
		position_rl  =(RelativeLayout) findViewById(R.id.position_rl);
		hospital_rl.setOnClickListener(this);
		department_rl.setOnClickListener(this);
		position_rl.setOnClickListener(this);
		
		
		hospital_tv = (TextView) findViewById(R.id.hospital_tv);
		department_tv = (TextView) findViewById(R.id.department_tv);
		title_tv = (TextView) findViewById(R.id.title_tv);
		
		zgzs_imv = (ImageView) findViewById(R.id.zgzs_imv);
		zyzs_imv = (ImageView) findViewById(R.id.zyzs_imv);
		gzz_imv = (ImageView) findViewById(R.id.gzz_imv);
		
	    imageLoader.displayImage("http://115.28.222.1/yijiaren/pics/certs/"+qc_path, zgzs_imv, options);

	    imageLoader.displayImage("http://115.28.222.1/yijiaren/pics/certs/"+pc_path, zyzs_imv, options);

	    imageLoader.displayImage("http://115.28.222.1/yijiaren/pics/certs/"+wc_path, gzz_imv, options);


		if(hospital!=null)
			hospital_tv.setText(hospital);
		if(department!=null)
			department_tv.setText(department);
		if(title!=null)
			title_tv.setText(title);
		
		applycheck = (TextView) title_bar.findViewById(R.id.applycheck);
		if(status.equals(Constants.DOCTOR_STATUS_DEFAULT)){
			applycheck.setText("申请审核");
			applycheck.setClickable(true);
		}else if(status.equals(Constants.DOCTOR_STATUS_PASSED)){
			applycheck.setText("审核通过");
			applycheck.setClickable(false);
		}else if(status.equals(Constants.DOCTOR_STATUS_PENDING)){
			applycheck.setText("审核中");
			applycheck.setClickable(false);
		}else{
			applycheck.setText("审核未通过");
			applycheck.setClickable(false);
		}
	}


	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		Intent intent;
		switch(v.getId()){
		case R.id.zgzs:
			intent  = new Intent(CareerInfoActivity.this, UpdateZgzsActivity.class);
			intent.putExtra("userData", userData.toString());
			startActivity(intent);
			break;
		case R.id.zyzs:
			intent  = new Intent(CareerInfoActivity.this, UpdateZyzsActivity.class);
			intent.putExtra("userData", userData.toString());
			startActivity(intent);
			break;
		case R.id.gzz:
			intent  = new Intent(CareerInfoActivity.this, UpdateGzzActivity.class);
			intent.putExtra("userData", userData.toString());
			startActivity(intent);
			break;
		case R.id.tdjs_rl:
			intent  = new Intent(CareerInfoActivity.this, TeamInfoActivity.class);
			startActivity(intent);
			break;
		case R.id.zytc_rl:
			intent  = new Intent(CareerInfoActivity.this, UpdateSkillActivity.class);
			startActivity(intent);
			break;
		case R.id.xw_rl:
			intent  = new Intent(CareerInfoActivity.this, UpdateDegreeActivity.class);
			startActivityForResult(intent, DEGREE);
			break;
		case R.id.yx_rl:
			intent  = new Intent(CareerInfoActivity.this, UpdateEmailActivity.class);
			startActivity(intent);
			break;
		case R.id.hospital_rl:
			intent  = new Intent(CareerInfoActivity.this, UpdateHospitalActivity.class);
			startActivityForResult(intent, HOSPITAL);
			break;
		case R.id.department_rl:
			intent  = new Intent(CareerInfoActivity.this, UpdateDepartmentActivity.class);
			intent.putExtra("hospital_id", hospital_id);
			startActivityForResult(intent, DEPARTMENT);
			break;
		case R.id.position_rl:
			intent  = new Intent(CareerInfoActivity.this, UpdateCategoryActivity.class);
			startActivityForResult(intent, CATEGORY);
			break;
		case R.id.applycheck:
			applyCheck();
			break;
		default:
			break;
		}
		
	}
	
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
	      switch (requestCode) {
	      case DEGREE:
	    	  
	    	  break;
	      case HOSPITAL:
	    	  hospital_id = data.getStringExtra("hospital_id");
	    	  hospital = data.getStringExtra("hospital_name");
	    	  hospital_tv.setText(hospital);
	    	  spHelper.putString("hospital_id", hospital_id);
	    	  break;
	      case DEPARTMENT:
	    	  department = data.getStringExtra("department_name");
	    	  department_tv.setText(department);
	    	  break;
	      case CATEGORY:
	    	  category = data.getStringExtra("category");
	    	  title_type = data.getStringExtra("title_type");
	  		  try {
				title = initData.getJSONObject("codings").getJSONObject("doctorCategory").getJSONObject(category).getJSONObject("title").getString(title_type);
				title_tv.setText(title);
	  		  } catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	  break;
	      default:  
	          break;  

	      }  
	      super.onActivityResult(requestCode, resultCode, data);  
	  }  
	private void applyCheck(){
		YJRHttpClient.post(context,"/doctor/applyCheck", null, new JsonHttpResponseHandler(){
			 @Override  
	            public void onSuccess(JSONObject data) {  
	             	jsonInfo = data;
	             	Log.v("weiyi",data.toString());
	             	try {
						if(jsonInfo.getInt("result") == 1){
							Toast.makeText(context, "申请审核成功", Toast.LENGTH_LONG).show();
							applycheck.setText("审核中");
							applycheck.setClickable(false);
						}else{
							Toast.makeText(context, jsonInfo.getString("info"), Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }  
		});
	}

}